﻿using System;
using System.Windows.Forms;
using System.Reflection;

using Elike.Toolkit.Common;

namespace shopsoft.main.AppCommon
{
    /// <summary>
    /// MDI子窗体控制
    /// </summary>
    public class OpenMdiChild
    {
        #region ---MDI方式，配合TabStrip使用---

        /// <summary>
        /// 打开指定MDI子窗体类型的MDI子窗体
        /// </summary>
        /// <param name="openChildForm">MDI子窗体（注：若该窗体构造函数含有参数，则其Tag属性中需存储可以精确定位到该窗体句柄的唯一标识）</param>
        /// <param name="mdiParent">父窗体</param>
        public static void OpenWindow(Form openChildForm, Form mdiParent)
        {
            if (!ContainMdiChild(openChildForm, mdiParent))
            {
                //显示MDI子窗体 
                if (openChildForm != null)
                {
                    openChildForm.MdiParent = mdiParent;
                    openChildForm.WindowState = FormWindowState.Maximized;
                    if (mdiParent.ActiveMdiChild != null)
                    {
                        mdiParent.ActiveMdiChild.WindowState = FormWindowState.Minimized;
                    }
                    openChildForm.Show();
                    openChildForm.Focus();
                }
            }
        }

        /// <summary>
        /// 搜索特定类型的MDI子窗体在已打开的MDI子窗体集中是否存在
        /// </summary>
        /// <param name="openChildForm">MDI子窗体[其Tag属性中需存储该窗体的唯一标识对象，以实现窗口句柄的精确定位]（如：存储窗体构造函数参数值列表对象）</param>
        /// <param name="mdiParent">父窗体</param>
        /// <returns></returns>
        private static bool ContainMdiChild(Form openChildForm, Form mdiParent)
        {
            Form myMDIChild = null;
            foreach (Form f in mdiParent.MdiChildren)
            {
                //确定窗体类型
                if (f.GetType() == openChildForm.GetType())
                {
                    //有参构造窗体
                    if (f.Tag != null)
                    {
                        //判断参数对象
                        if (f.Tag.ToString() == openChildForm.Tag.ToString())
                        {
                            //找到MDI子窗体
                            myMDIChild = f;
                            break;
                        }
                    }
                    else//无参构造窗体
                    {
                        //找到MDI子窗体
                        myMDIChild = f;
                        break;
                    }
                   
                }
            }
            bool flag = false;
            //显示已存在的窗体
            if (myMDIChild != null)
            {
                myMDIChild.TopMost = true;
                myMDIChild.Show();
                myMDIChild.Focus();
                flag = true;
            }
            return flag;
        }

        #endregion ---MDI方式，配合TabStrip使用---

        #region ---普通窗体方式，配置TabControl使用---

        ///// <summary>
        ///// 在指定TabControl中打开指定窗体
        ///// </summary>
        ///// <param name="tabControl">TabControl对象</param>
        ///// <param name="childTypeString">窗体类型字符串</param>
        ///// <param name="isCloseButtonVisible">标签上是否显示关闭按钮</param>
        //public static void OpenTabWindow(TabControl tabControl, string childTypeString, bool isCloseButtonVisible)
        //{
        //    string fullName = tabControl.Parent.GetType().FullName;
        //    childTypeString = fullName.Substring(0, fullName.LastIndexOf('.') + 1) + childTypeString;
        //    string tabName = childTypeString.Split('.')[childTypeString.Split('.').Length - 1];
        //    if (!IsOpenTab(tabControl, tabName))
        //    {
        //        //获取当前进程集会
        //        Assembly assembly = Assembly.GetExecutingAssembly();

        //        //创建使用字符串类型的数据类型
        //        Type typForm = assembly.GetType(childTypeString);

        //        //创建对象使用类型的“InvokeMember”方法
        //        Object obj = typForm.InvokeMember(
        //            null,
        //            BindingFlags.DeclaredOnly |
        //            BindingFlags.Public | BindingFlags.NonPublic |
        //            BindingFlags.Instance | BindingFlags.CreateInstance,
        //            null,
        //            null,
        //            null);

        //        //显示MDI子窗体 
        //        if (obj != null)
        //        {
        //            Form frm = obj as Form;

        //            TabItem tabItem = tabControl.CreateTab(frm.Text);
        //            tabItem.Name = tabName;
        //            tabItem.CloseButtonVisible = isCloseButtonVisible;
        //            tabItem.Icon = frm.Icon;
        //            TabControlPanel tcp = new TabControlPanel();
        //            tabItem.AttachedControl = tcp;
        //            tcp.TabItem = tabItem;
        //            tcp.Dock = DockStyle.Fill;

        //            frm.TopLevel = false;
        //            frm.Dock = DockStyle.Fill;
        //            frm.FormBorderStyle = FormBorderStyle.None;
        //            frm.Show();
        //            tcp.Controls.Add(frm);

        //            tabControl.Controls.Add(tcp);
        //            tabControl.SelectedTab = tabItem;
        //        }
        //    }
        //}

        ///// <summary>  
        /////  检查是否已打开某个窗体  
        ///// </summary>
        ///// <param name="tabControl">TabControl对象</param>
        ///// <param name="tabName">标签名称</param>  
        ///// <returns></returns>  
        //private static bool IsOpenTab(TabControl tabControl, string tabName)
        //{
        //    bool isOpened = false;

        //    foreach (TabItem tab in tabControl.Tabs)
        //    {
        //        if (tab.Name.Trim() == tabName.Trim())
        //        {
        //            isOpened = true;
        //            tabControl.SelectedTab = tab;
        //            break;
        //        }
        //    }
        //    return isOpened;
        //}

        #endregion ---普通窗体方式，配置TabControl使用---
    }
}
